package org.yzh.web.endpoint;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.yzh.framework.annotation.Endpoint;
import org.yzh.framework.annotation.Mapping;
import org.yzh.framework.message.SyncFuture;
import org.yzh.framework.session.MessageManager;
import org.yzh.framework.session.Session;
import org.yzh.framework.session.SessionManager;
import org.yzh.web.waterMeter.common.WaterMeterFunctionId;
import org.yzh.web.waterMeter.common.WaterMeterMessageId;
import org.yzh.web.waterMeter.dto.CommonConfirm;
import org.yzh.web.waterMeter.dto.basics.WaterMeterMessage;

import java.nio.channels.SeekableByteChannel;
import java.util.concurrent.TimeUnit;

@Endpoint
@Component
public class WaterMeterEndpoint {

    private static final Logger logger = LoggerFactory.getLogger(WaterMeterEndpoint.class.getSimpleName());

    private SessionManager sessionManager = SessionManager.getInstance();

    private MessageManager messageManager = MessageManager.INSTANCE;

    /**
     * 异步请求
     * @param message
     * @return
     */
    public Object ayncSend(WaterMeterMessage message) {
        String hubId = message.getHubId();
        Session session = sessionManager.getByMobileNumber(hubId);

        session.getChannel().writeAndFlush(message);

        return null;
    }

    @Mapping(types = WaterMeterMessageId.WaterMeterReading, desc = "历史数据")
    public void waterMeterReading(WaterMeterMessage<org.yzh.web.waterMeter.dto.WaterMeterReading> message) {
        org.yzh.web.waterMeter.dto.WaterMeterReading body = message.getBody();
        String hubId = message.getHubId();
        System.out.println("----------历史数据----------" +  message.getHubId());

    }


    @Mapping(types = WaterMeterMessageId.Login, desc = "终端登录")
    public WaterMeterMessage login(WaterMeterMessage<CommonConfirm> message, Session session) {
        session.setTerminalId(message.getHubId());
        sessionManager.put(Session.buildId(session.getChannel()), session);
        System.out.println("----------终端登录----------");
        return new WaterMeterMessage(message, WaterMeterFunctionId.CommonConfirm,0x0B,
                message.getType());
    }

    @Mapping(types = WaterMeterMessageId.HeartBeat, desc = "终端心跳")
    public WaterMeterMessage heartBeat(WaterMeterMessage<CommonConfirm> message) {
        return new WaterMeterMessage(message, WaterMeterFunctionId.CommonConfirm,0x0B,
                message.getType());
    }



}